package cn.deschen.search.index;

import com.baomidou.mybatisplus.annotation.TableLogic;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

/**
 * @Author hanbin_chen
 * @Description 索引对象
 * @DateTime 2021/6/5
 * @Version V1.0.0
 */
@Data
@Slf4j
public class IndexObject<T extends IndexObject> {

    @ApiModelProperty(value = "是否删除")
    @TableLogic(value = "0", delval = "1")
    private Integer isDel;

    public Boolean isDeleted() {
        return isDel == 0 ? false : true;
    }

    public void update(T newValue) {
        Field[] fields = this.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                if (null != field.get(newValue)) {
                    field.set(this, field.get(newValue));
                }
            }
        } catch (Exception e) {
            log.error("索引更新异常:{}\n class:{}\n oldValue={}\n newValue={}",
                    e.getMessage(),
                    this.getClass(),
                    this,
                    newValue);
        }

    }

    ;
}
